import os
import logging

from google.appengine.api import users

from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

import django
from django import http
from django import shortcuts

import views

from models import Greeting
from models import Website
from models import GreetingForm

def index(request):
  """Request / -- show all greetings."""
  user = users.GetCurrentUser()
  greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY created DESC LIMIT 50")
  websites = db.GqlQuery("SELECT * FROM Website ORDER BY created DESC LIMIT 5")
  
  return views.respond(request, user, 'greetings/index',
                       {'greetings': greetings, 'websites': websites})
  

def create(request, website_id):
  """Create a greeting.  GET shows a blank form, POST processes it."""
  
  user = users.GetCurrentUser()
  if user is None:
    # Now make them login.
    # See what we did in websites.py - this should happen here as well.
    login_url = users.create_login_url(request.get_full_path())
    return http.HttpResponseRedirect(login_url)
  
  if website_id is None:
    return http.HttpResponseNotFound('No website exists with that key')
    
  website = Website.get(db.Key.from_path(Website.kind(), int(website_id)))
  if website is None:
    return http.HttpResponseNotFound('No website exists with that key (%r)' %
                                     website_id)
    
  form = GreetingForm(data=request.POST or None)

  if not request.POST:
    return views.respond(request, user, 'greetings/form', 
                         {'form': form, 'website': website})
    
  errors = form.errors
  if not errors:
    try:
      greeting = form.save(commit=False)
    except ValueError, err:
      errors['__all__'] = unicode(err)
  if errors:
    return views.respond(request, user, 'greetings/form', 
                         {'form': form, 'website': website})
    
  greeting.website = website
  greeting.author = user
  greeting.put()
  
  url = '/websites/view/%s' % website.key().id()
  logging.info('Saved the greeting, sending to %s' % url)
  return http.HttpResponseRedirect(url)


def edit(request, greeting_id):
  """Create or edit a gift.  GET shows a blank form, POST processes it."""
  user = users.GetCurrentUser()
  if user is None:
    return http.HttpResponseForbidden('You must be signed in to add or edit a greeting')

  greeting = None
  if greeting_id:
    greeting = models.greetings.Greeting.get(db.Key.from_path(Greeting.kind(), int(greeting_id)))
    if greeting is None:
      return http.HttpResponseNotFound('No greeting exists with that key (%r)' %
                                       greeting_id)

  form = models.GreetingForm(data=request.POST or None, instance=greeting)

  if not request.POST:
    return views.respond(request, user, 'greeting', {'form': form, 'greeting': greeting})

  errors = form.errors
  if not errors:
    try:
      greeting = form.save(commit=False)
    except ValueError, err:
      errors['__all__'] = unicode(err)
  if errors:
    return views.respond(request, user, 'greeting', {'form': form, 'greeting': greeting})

  greeting.put()

  return http.HttpResponseRedirect('/')
